console.log("详细验证所有接口的返回字段格式...");
const http = require('http');

async function testInterface(url, method = 'GET', data = null, headers = {}) {
    return new Promise((resolve, reject) => {
        const options = {
            hostname: 'localhost',
            port: 8000,
            path: url,
            method: method,
            headers: {
                'Content-Type': method === 'POST' ? 'application/x-www-form-urlencoded' : 'application/json',
                ...headers
            }
        };

        if (data && method === 'POST') {
            options.headers['Content-Length'] = Buffer.byteLength(data);
        }

        const req = http.request(options, (res) => {
            let responseData = '';
            res.on('data', (chunk) => {
                responseData += chunk;
            });
            res.on('end', () => {
                try {
                    const jsonData = JSON.parse(responseData);
                    resolve({ status: res.statusCode, data: jsonData });
                } catch (e) {
                    resolve({ status: res.statusCode, data: responseData });
                }
            });
        });

        req.on('error', (e) => {
            reject(e.message);
        });

        req.setTimeout(5000, () => {
            req.destroy();
            reject('Timeout');
        });

        if (data && method === 'POST') {
            req.write(data);
        }

        req.end();
    });
}

async function validateAPI() {
    console.log('=== 前端接口验证 ===\n');
    
    // 接口1: 文章搜索
    console.log('1. 文章搜索 (/index/search)');
    try {
        const result = await testInterface('/api/v1/index/search');
        const requiredFields = ['pages', 'page', 'data'];
        const dataRequiredFields = ['id', 'title', 'intro', 'cover', 'category', 'read', 'comment', 'date'];
        
        console.log(`状态码: ${result.status}`);
        console.log(`顶级字段: ${Object.keys(result.data).join(', ')}`);
        if (result.data.data && result.data.data.length > 0) {
            console.log(`数据字段: ${Object.keys(result.data.data[0]).join(', ')}`);
            const hasAllFields = dataRequiredFields.every(field => result.data.data[0].hasOwnProperty(field));
            console.log(`字段完整性: ${hasAllFields ? '✓' : '✗'}`);
        }
    } catch (e) {
        console.log(`错误: ${e}`);
    }
    console.log('');

    // 接口2: 文章类型
    console.log('2. 文章类型 (/index/category)');
    try {
        const result = await testInterface('/api/v1/index/category');
        console.log(`状态码: ${result.status}`);
        console.log(`顶级字段: ${Object.keys(result.data).join(', ')}`);
        if (result.data.data && result.data.data.length > 0) {
            console.log(`数据字段: ${Object.keys(result.data.data[0]).join(', ')}`);
            const hasRequiredFields = result.data.data[0].hasOwnProperty('id') && result.data.data[0].hasOwnProperty('name');
            console.log(`字段完整性: ${hasRequiredFields ? '✓' : '✗'}`);
        }
    } catch (e) {
        console.log(`错误: ${e}`);
    }
    console.log('');

    // 接口3: 热点图片
    console.log('3. 热点图片 (/index/hotpic)');
    try {
        const result = await testInterface('/api/v1/index/hotpic');
        console.log(`状态码: ${result.status}`);
        console.log(`顶级字段: ${Object.keys(result.data).join(', ')}`);
        if (result.data.data && result.data.data.length > 0) {
            console.log(`数据字段: ${Object.keys(result.data.data[0]).join(', ')}`);
            console.log(`数据条数: ${result.data.data.length} (应该是5条)`);
            const hasRequiredFields = ['id', 'cover', 'title'].every(field => result.data.data[0].hasOwnProperty(field));
            console.log(`字段完整性: ${hasRequiredFields ? '✓' : '✗'}`);
        }
    } catch (e) {
        console.log(`错误: ${e}`);
    }
    console.log('');

    // 接口4: 文章热门排行
    console.log('4. 文章热门排行 (/index/rank)');
    try {
        const result = await testInterface('/api/v1/index/rank');
        console.log(`状态码: ${result.status}`);
        console.log(`顶级字段: ${Object.keys(result.data).join(', ')}`);
        if (result.data.data && result.data.data.length > 0) {
            console.log(`数据字段: ${Object.keys(result.data.data[0]).join(', ')}`);
            console.log(`数据条数: ${result.data.data.length} (应该是7条)`);
            const hasRequiredFields = ['id', 'title'].every(field => result.data.data[0].hasOwnProperty(field));
            console.log(`字段完整性: ${hasRequiredFields ? '✓' : '✗'}`);
        }
    } catch (e) {
        console.log(`错误: ${e}`);
    }
    console.log('');

    // 接口5: 最新资讯
    console.log('5. 最新资讯 (/index/latest)');
    try {
        const result = await testInterface('/api/v1/index/latest');
        const requiredFields = ['id', 'title', 'intro', 'cover', 'category', 'read', 'comments', 'date'];
        
        console.log(`状态码: ${result.status}`);
        console.log(`顶级字段: ${Object.keys(result.data).join(', ')}`);
        if (result.data.data && result.data.data.length > 0) {
            console.log(`数据字段: ${Object.keys(result.data.data[0]).join(', ')}`);
            console.log(`数据条数: ${result.data.data.length} (应该是5条)`);
            const hasAllFields = requiredFields.every(field => result.data.data[0].hasOwnProperty(field));
            console.log(`字段完整性: ${hasAllFields ? '✓' : '✗'}`);
            
            // 检查具体字段
            const sample = result.data.data[0];
            console.log(`  - intro字段存在: ${sample.hasOwnProperty('intro') ? '✓' : '✗'}`);
            console.log(`  - comments字段存在: ${sample.hasOwnProperty('comments') ? '✓' : '✗'}`);
            console.log(`  - category字段存在: ${sample.hasOwnProperty('category') ? '✓' : '✗'}`);
        }
    } catch (e) {
        console.log(`错误: ${e}`);
    }
    console.log('');

    // 接口6: 最新评论
    console.log('6. 最新评论 (/index/latest_comment)');
    try {
        const result = await testInterface('/api/v1/index/latest_comment');
        console.log(`状态码: ${result.status}`);
        console.log(`顶级字段: ${Object.keys(result.data).join(', ')}`);
        if (result.data.data && result.data.data.length > 0) {
            console.log(`数据字段: ${Object.keys(result.data.data[0]).join(', ')}`);
            console.log(`数据条数: ${result.data.data.length} (应该是6条)`);
            const hasRequiredFields = ['author', 'date', 'intro'].every(field => result.data.data[0].hasOwnProperty(field));
            console.log(`字段完整性: ${hasRequiredFields ? '✓' : '✗'}`);
        }
    } catch (e) {
        console.log(`错误: ${e}`);
    }
    console.log('');

    // 接口7: 焦点关注
    console.log('7. 焦点关注 (/index/attention)');
    try {
        const result = await testInterface('/api/v1/index/attention');
        console.log(`状态码: ${result.status}`);
        console.log(`顶级字段: ${Object.keys(result.data).join(', ')}`);
        if (result.data.data && result.data.data.length > 0) {
            console.log(`数据字段: ${Object.keys(result.data.data[0]).join(', ')}`);
            console.log(`数据条数: ${result.data.data.length} (应该是7条)`);
            const hasRequiredFields = result.data.data[0].hasOwnProperty('intro');
            console.log(`字段完整性: ${hasRequiredFields ? '✓' : '✗'}`);
        }
    } catch (e) {
        console.log(`错误: ${e}`);
    }
    console.log('');

    // 接口8: 文章详细内容
    console.log('8. 文章详细内容 (/index/article?id=127)');
    try {
        const result = await testInterface('/api/v1/index/article?id=127');
        const requiredFields = ['title', 'author', 'type', 'date', 'read', 'comments', 'content', 'prev', 'next'];
        
        console.log(`状态码: ${result.status}`);
        console.log(`顶级字段: ${Object.keys(result.data).join(', ')}`);
        if (result.data.data) {
            console.log(`数据字段: ${Object.keys(result.data.data).join(', ')}`);
            const hasAllFields = requiredFields.every(field => result.data.data.hasOwnProperty(field));
            console.log(`字段完整性: ${hasAllFields ? '✓' : '✗'}`);
        }
    } catch (e) {
        console.log(`错误: ${e}`);
    }
    console.log('');

    console.log('=== 测试完成 ===');
    process.exit(0);
}

validateAPI().catch(console.error);
